package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.BuyDetailBean;
import com.project.bean.BuyOrderBean;
import com.project.bean.LogBean;
import com.project.mapper.IBuyOrderMapper;
import com.project.mapper.ILogMapper;
import com.project.service.IBuyOrderService;
import com.project.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

@Service
@Transactional
public class BuyOrderService implements IBuyOrderService {
    @Autowired
//   private  buyOrderService;
    private IBuyOrderMapper buyOrderMapper;

    @Override
    public IPage<BuyOrderBean> findByItem(Integer pageNO, String supplierName, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<BuyOrderBean> qw = new QueryWrapper<>();
        if (supplierName != null && !supplierName.equals("")) {
            qw.like("o_buyUserName", supplierName);
        }
        if (startDate != null) {
            qw.ge("o_date", startDate);
        }
        if (endDate != null) {
            qw.le("o_date", endDate);
        }
        return buyOrderMapper.selectPage(new Page<>(pageNO, 2), qw);
    }

    @Override
    public BuyOrderBean findById(Integer orderId) {
        BuyOrderBean buyOrderBean = buyOrderMapper.selectById(orderId);
        buyOrderBean.setBuyDetailList(buyOrderMapper.findById(orderId));

        return buyOrderBean;
    }

    @Override
    public Integer add(String supplierName, String realName) {
        BuyOrderBean buyOrderBean = new BuyOrderBean(supplierName, realName);
        buyOrderMapper.insert(buyOrderBean);
        return buyOrderBean.getId();
    }
}
